<?php
namespace app\models;
use Yii;

class Orderinfo
{
	public static function getIdByOrdernum($ordernum)
	{
		$query = new \yii\db\Query();

		$orderinfoList = $query->select("orderinfoid as orderid")
				  ->from("JXC_ORDERINFO")
				  ->where("ordernum like '%".$ordernum."%'")
				  ->all();

		$result = array();

		foreach($orderinfoList as $orderinfo){
			$result[] = $orderinfo['orderid'];
		}

		return implode(",",$result);
	}

	public static function getIdFromItem($productname,$drawingno)
	{
		$query = new \yii\db\Query();

		$orderinfoList = $query->select("orderinfoid")
				  ->from("JXC_ORDERITEM")
				  ->where("productname like '".$productname."'")
				  ->andWhere("drawingno like '%".$drawingno."%'")
				  ->all();


		$result = array();

		foreach($orderinfoList as $orderInfo){
			$result[] = $orderInfo['orderinfoid'];
		}

		return implode(",",$result);
	}

	public static function getProductidByOrdernum($ordernum)
	{
		$query = new \yii\db\Query();

		$orderidList = $query->select("orderinfoid")
				  ->from("JXC_ORDERINFO")
				  ->where("ordernum like '%".$ordernum."%'")
				  ->all();

		$result = array();
		foreach($orderidList as $orderid){
			$result[] = $orderid['orderinfoid'];
		}
		$orderidList = implode(",",$result);

		$productidList = array();

		if (empty($orderidList)){
			$productidList = $query->select("productid")
					->from("JXC_ORDERITEM")
					->where("orderinfoid = 0")
					->all();
		}else{
			$productidList = $query->select("productid")
					->from("JXC_ORDERITEM")
					->where("orderinfoid in (".$orderidList.")")
					->all();
		}

		$result = array();
		foreach($productidList as $productid){
			$result[] = $productid['productid'];
		}

		return implode(",",$result);
	}

	public static function getOrderinfoidForOutstock($ordernum,$orderunit,$receivingunit)
	{
		$query = new \yii\db\Query();

		$orderidList = $query->select("orderinfoid")
				  ->from("JXC_ORDERINFO")
				  ->where("ordernum like '%".$ordernum."%'")
				  ->andWhere("orderunit like '%".$orderunit."%'")
				  ->andWhere("receivingunit like '%".$receivingunit."%'")
				  ->all();

		$result = array();

		foreach($orderidList as $orderid){
			$result[] = $orderid['orderinfoid'];
		}

		return implode(",", $result);
	}
}